package com.lemon.smartsafetynew.ui.activity.patrol;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolHistoryItemVo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.adapter.patrol.PatrolHistoryAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/8/3.
 */

public class SafePatrolHistoryActivity extends SwipeBackActivity
        implements RecyclerTClickListener<SafePatrolHistoryItemVo> {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.history_date_label)
    TextView historyDateLabel;

    private LemonRcvAdapter<SafePatrolHistoryItemVo> mAdapter;
    private List<SafePatrolHistoryItemVo> mDataList = new ArrayList<>();

    private static String item_id;

    @Override
    protected void initTitle() {
        this.setBarTitle("巡查历史");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_patrol_history;
    }

    @Override
    protected void getBundleArgs() {

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            item_id = bundle.getString(KeyConstants.KEY_SAFE_PATROL_ITEM_ID);
        } else {
            callFinish();
        }
    }

    @Override
    protected void initViews() {
        super.initViews();

        initRecyclerView();

        Calendar now = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
        String dateStr = dateFormat.format(now.getTime());
        historyDateLabel.setText(dateStr);

        // ========= get net data =======
        callGetHistory();
    }

    private void initRecyclerView() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new PatrolHistoryAdapter(mDataList, this);
        recyclerView.setAdapter(mAdapter);

    }


    @Override
    public void onItemClick(SafePatrolHistoryItemVo data, int position) {

        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_CHECK_ITEM_HISTORY_RESULT, data);
        UIHelper.showActivity(this, bundle, SafePatrolHistoryDetailActivity.class);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setIconEnable(menu, true);
        getMenuInflater().inflate(R.menu.menu_calendar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_calendar: {
                pickCheckDate();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());
                historyDateLabel.setText(dateStr);


                callGetHistory();

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_PATROL_CHECK_ITEM_HISTORY_LIST.equals(event.getTag())) {

            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();

                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (mAdapter != null) {
                        mDataList.clear();
                        mAdapter.notifyDataSetChanged();
                        mAdapter.addItems((List<SafePatrolHistoryItemVo>) event.getObj());
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                }
            }

        }

    }

    private void callGetHistory() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("item_id", item_id);
        params.put("date", historyDateLabel.getText().toString());

        SafePatrolServices.init().getSafePatrolCheckItemHistoryList(params);
    }

}
